<?php
namespace app\home\controller;

use app\common\model\Nav;
use app\common\model\Theme;
use think\App;
use think\Controller;
use think\facade\Config;
use think\facade\Env;


class Base extends Controller
{
//    public function __construct(App $app = null)
//    {
////        config('template.view_base', getThemePath());
//
//        parent::__construct($app);
//    }


    protected function fetchHtml()
    {
        controller('common/JKBuildHtml')->buildFromFetch( $html = $this->fetch(), input('get.') );
        return $html;
    }
    public function template($path, $vars = [])
    {
        cookie('current_path', '/'.$path);
        $vars['_url_crumbs'] = Nav::getCrumbs('/'.$path);
        $nav_info = Nav::where('path','/'.$path)->find();
        $vars['_url_gid'] = '';
        if($nav_info) {
            if($nav_info['page_type'] == 'diy' || $nav_info['page_type'] == '') {
                $path = $nav_info['path'];
            } else {
                $path = $nav_info['page_type'] . '/' . ( $nav_info['temp_name'] ? $nav_info['temp_name'] : 'a' );
            }
            if($nav_info['params']) $vars['_params'] = [$nav_info['params']];
            if($nav_info['gid']) {
                $vars['_url_gid'] = $nav_info['gid'];
                $vars['_nav_header'] = [
                    'title' => $nav_info['title'],
                    'seo_keyword' => $nav_info['seo_keyword'],
                    'seo_desc' => $nav_info['seo_desc'],
                ];
            }
        }
//        if($this->checkTemplateFile($path)) {
//            $this->error("模板不存在");
//        }
        return $this->theme()->fetch($path, $vars);
    }
    public function theme(){
        $this->view->config('view_path', Env::get('root_path') . 'template/'. getThemePath() .'/');
        return $this;
    }
    private function checkTemplateFile($path)
    {
        echo TEMPLATE_PATH . $path . ".html";
        return file_exists(TEMPLATE_PATH . $path . ".html");
    }

    protected function handleValidate($data, $rules)
    {
        $validate = new \think\Validate;
        $validate->rule($rules);
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
    }
}
